﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Entities;
using BAL.Utils;
using BAL.Components;
using DigitalAnts.App_Code.BAL.Utils;
using System.Data;
using DigitalAnts.App_Code.BAL.Components;
using VMCLibrary.RegexServiceProvider;

namespace DigitalAnts.Admin
{
    public partial class Employee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Login() == false)
            {
                Response.Redirect("~/Admin/EmployeeLogin.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    lbltitle.Text = "Bạn đang thêm thông tin";
                    showEmployeeList();
                    showUpdate();
                }
            }
        }

        public bool Login()
        {
            clsEmployee obj = new clsEmployee();
            obj.UserName = Session["AdminUser"].ToString();
            obj.Role = Session["Role"].ToString();

            return clsEmployeeLoginBAL.queryEmployeeLogin(obj);
        }

        public void addNewEmployee()
        {
            RegexSpecificConfigManager validateUser = new RegexSpecificConfigManager();
            List<bool> val = new List<bool>();
            val.Add(validateUser.ValidateCustomUserName(txtusername.Text.Trim()));
            val.Add(validateUser.ValidateCustomPassword(txtpassword.Text.Trim()));
            val.Add(validateUser.ValidateCustomEmail(txtemail.Text.Trim()));
            val.Add(validateUser.ValidateCustomCellPhoneNumber(txtcellphone.Text.Trim()));
            val.Add(validateUser.ValidateCustomAddress(txtaddress.Text.Trim()));
            val.Add(validateUser.ValidateCustomPhoneNumber(txthomephone.Text.Trim()));
            if (val.IndexOf(false) >= 0) { lblmess.Text = "Dữ liệu lỗi"; }
            else{
            clsEmployee obj = new clsEmployee();
            obj.UserName = txtusername.Text.Trim();
            obj.Password = clsSecurity.doEncryptPasswordWithMD5(txtpassword.Text.Trim());
            obj.Email = txtemail.Text.Trim();
            obj.Avatar = fulavatar.FileName;
            obj.PhoneNumber = txtcellphone.Text.Trim();
            obj.Address = txtaddress.Text.Trim();
            obj.HomePhone = txthomephone.Text.Trim();

            if (clsEmployeeBAL.doAddNewEmployee(obj) == true && clsUploadImage.uploadProductImage(fulavatar) == true)
            {
                lblmess.Text = "Bạn đã thêm thông tin thành công";
                showEmployeeList();
            }
            else { lblmess.Text = "Bạn đã thêm thông tin thất bại"; }
        }
        }
        protected void bttsave_Click(object sender, EventArgs e)
        {
            if (Request["EmployeeID"] == null){
                addNewEmployee();
            }
            else
            { 
            //    RegexSpecificConfigManager validateUser = new RegexSpecificConfigManager();
            //    List<bool> val = new List<bool>();
            //    val.Add(validateUser.ValidateCustomEmail(txtemail.Text.Trim()));
            //    val.Add(validateUser.ValidateCustomCellPhoneNumber(txtcellphone.Text.Trim()));
            //    val.Add(validateUser.ValidateCustomAddress(txtaddress.Text.Trim()));
            //    val.Add(validateUser.ValidateCustomPhoneNumber(txthomephone.Text.Trim()));
            //    if (val.IndexOf(false) >= 0) { lblmess.Text = "Dữ liệu lỗi"; }
            //else{
                clsEmployee obj = new clsEmployee();
                if (fulavatar.FileName != null)
            {
                obj.Avatar = fulavatar.FileName;
                clsUploadImage.uploadProductImage(fulavatar);
               
            }
                    
                    obj.EmployeeID = Convert.ToInt32(Request["EmployeeID"].ToString());
                    obj.Email = txtemail.Text.Trim();
                    obj.PhoneNumber = txtcellphone.Text.Trim();
                    obj.Address = txtaddress.Text.Trim();
                    obj.HomePhone = txthomephone.Text.Trim();

                    if(clsEmployeeBAL.doUpdateEmployee(obj) == true )
                    {
                        lblmess.Text = "Cập nhật thông tin thành công";
                        showEmployeeList();
                    }
                    else{
                        lblmess.Text = "Cập nhật thông tin thất bại";
                    }
                }
            //}
        }

        public void showEmployeeList()
        {
            gvEmployee.DataSource = clsEmployeeBAL.doEmployeeSelectList();
            gvEmployee.DataBind();
        }

        public void showUpdate()
        { 
            if(Request["EmployeeID"] != null)
            {
                lbltitle.Text = "Bạn đang sửa thông tin";
                bttsave.Text = "Sửa";
                txtpassword.Enabled = false;
                txtusername.Enabled = false;
                clsEmployee obj = new clsEmployee();
                obj.EmployeeID = Convert.ToInt32(Request["EmployeeID"].ToString());

                DataTable dt = clsEmployeeBAL.doEmployeeSelectListById(obj);
                hddavatar.Value = dt.Rows[0]["colAvatar"].ToString();
                txtpassword.Text = dt.Rows[0]["colPassword"].ToString();
                txtusername.Text = dt.Rows[0]["colUsername"].ToString();
                txtemail.Text = dt.Rows[0]["colEmail"].ToString();
                txtcellphone.Text = dt.Rows[0]["colCellPhone"].ToString();
                txtaddress.Text = dt.Rows[0]["colAddress"].ToString();
                txthomephone.Text = dt.Rows[0]["colHomePhone"].ToString();
            }
        }

        protected void gvEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEmployee.PageIndex = e.NewPageIndex;
            gvEmployee.DataBind();
        }
    }
}